package com.example.backend.service;

import com.example.backend.mapper.UserInfoMapper;
import com.example.backend.model.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
@Service
public class UserService {


    @Autowired
    private UserInfoMapper userInfoMapper;

    @Transactional
    public Long addUser(UserInfo userInfo) {
        // 验证PID唯一性
        if (userInfoMapper.selectByPID(userInfo.getPID()) != null) {
            throw new RuntimeException("身份证号已存在，无法新增客户");
        }
        // 验证电话唯一性
        if (userInfo.getTelephone() != null && userInfoMapper.selectByTelephone(userInfo.getTelephone()) != null) {
            throw new RuntimeException("联系电话已存在，无法新增客户");
        }
        userInfoMapper.insert(userInfo);
        return userInfo.getCustomerID();
    }



    /**
     * 根据ID查询客户
     */
    public UserInfo getUserById(Long customerID) {
        return userInfoMapper.selectByPrimaryKey(customerID);
    }

    /**
     * 更新客户信息（含唯一性验证）
     */
    @Transactional
    public boolean updateUser(UserInfo userInfo) {
        // 验证客户存在性
        UserInfo existUser = userInfoMapper.selectByPrimaryKey(userInfo.getCustomerID());
        if (existUser == null) {
            throw new RuntimeException("客户不存在，更新失败");
        }

        // 检查PID是否被其他客户使用
        if (userInfo.getPID() != null && !userInfo.getPID().equals(existUser.getPID())) {
            UserInfo pidUser = userInfoMapper.selectByPID(userInfo.getPID());
            if (pidUser != null && !pidUser.getCustomerID().equals(userInfo.getCustomerID())) {
                throw new RuntimeException("身份证号已被其他客户使用");
            }
        }

        // 检查电话是否被其他客户使用
        if (userInfo.getTelephone() != null && !userInfo.getTelephone().equals(existUser.getTelephone())) {
            UserInfo telUser = userInfoMapper.selectByTelephone(userInfo.getTelephone());
            if (telUser != null && !telUser.getCustomerID().equals(userInfo.getCustomerID())) {
                throw new RuntimeException("联系电话已被其他客户使用");
            }
        }

        return userInfoMapper.updateByPrimaryKey(userInfo) > 0;
    }

    /**
     * 删除客户（逻辑删除或物理删除）
     */
    @Transactional
    public boolean deleteUser(Long customerID) {
        // 验证客户存在性
        UserInfo userInfo = userInfoMapper.selectByPrimaryKey(customerID);
        if (userInfo == null) {
            throw new RuntimeException("客户不存在，删除失败");
        }
        return userInfoMapper.deleteByPrimaryKey(customerID) > 0;
    }

    /**
     * 按PID精确查询
     */
    public UserInfo getUserByPID(String PID) {
        return userInfoMapper.selectByPID(PID);
    }

    /**
     * 模糊查询（姓名/电话）
     */
    public List<UserInfo> searchUsersByKeyword(String keyword) {
        return userInfoMapper.selectByKeyword(keyword);
    }

    /**
     * 地址范围查询
     */
    public List<UserInfo> searchUsersByAddress(String address) {
        return userInfoMapper.selectByAddressLike(address);
    }

    /**
     * 按电话精确查询
     */
    public UserInfo getUserByTelephone(String telephone) {
        return userInfoMapper.selectByTelephone(telephone);
    }
}